

-- Namespace
	local _,mod = ...


-- Frame creations
	mod.units = {}
	
	local function OnEnter(self)
		GameTooltip_SetDefaultAnchor(GameTooltip,self)
		GameTooltip:SetUnit(self.unit)
	end
	
	local function OnLeave(self)
		GameTooltip:ClearLines()
		GameTooltip:Hide()
	end
	
	local function showmenu(self)
		local menu
		local unit = self.unit
		local id = unit:match('^party(%d)$')
		if id then
			menu = _G['PartyMemberFrame'..id..'DropDown']
		else
			menu = _G[unit:gsub('.',string.upper,1)..'FrameDropDown']
		end
		if menu then
			ToggleDropDownMenu(1,nil,menu,'cursor')
		end
	end
	
	local click_cast = ClickCastFrames
	if not click_cast then
		click_cast = {}
		ClickCastFrames = click_cast
	end
	
	function mod:UF(unit)
		local frame = self.units[unit]
		if frame then
			return frame,false
		end
		
		frame = mod:CreateFrame('Button','nfUF:'..unit,UIParent,'SecureActionButtonTemplate')
		
		frame.unit = unit
		frame:SetAttribute('unit',unit)
		self.units[unit] = frame
		
		frame:RegisterForClicks('AnyDown')
		frame:SetAttribute('*type1','target')
		frame:SetAttribute('*type2','menu')
		frame:SetAttribute('*type3','focus')
		
		RegisterUnitWatch(frame)
		
		frame.menu = showmenu
		frame:SetScript('OnEnter',OnEnter)
		frame:SetScript('OnLeave',OnLeave)
		
		click_cast[frame] = true
		
		return frame,true
	end


-- Care tables
	-- Returns a table containing all units that care about a unitevent, or nil
	-- if none do.
	function mod:unitevent_care(event)
		local c = {}
		for unit,frame in pairs(self.units) do
			if frame:HasUpdate(event) then
				c[unit] = true
			end
		end
		if next(c) then
			return c
		end
	end
